package com.hs.panghu.servlet;

import com.alibaba.fastjson.JSON;
import com.hs.panghu.dao.ShopDao;
import com.hs.panghu.dao.impl.ShopDaoImpl;
import com.hs.panghu.entity.Shop;
import com.hs.panghu.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/shop")
public class ShopServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        ShopDao shopDao = new ShopDaoImpl();
        if ("querySearchShopList".equals(action)) {
            String shopName = req.getParameter("keyword");
            String startTime = req.getParameter("startTime");
            String endTime = req.getParameter("endTime");
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            String shopStatus = req.getParameter("shopStatus");
            String phone = req.getParameter("phone");

            Map<String, String> map = new HashMap<>();
            map.put("keyword", shopName);
            map.put("startTime", startTime);
            map.put("endTime", endTime);
            map.put("pageNum", pageNum);
            map.put("pageSize", pageSize);
            map.put("shopStatus", shopStatus);
            map.put("phone", phone);


            List<Shop> list = shopDao.selectShops(map);

            int count = shopDao.selectShopCount(map);

            Map map1 = new HashMap();
            map1.put("list", list);
            map1.put("total", count);

            Result success = Result.success(map1);

            resp.getWriter().write(JSON.toJSONString(success));

        } else if ("addShop".equals(action)) {
            String shopName = req.getParameter("shopName");
            String shopDescribe = req.getParameter("shopDescribe");
            String shopImage = req.getParameter("shopImage");
            String shopPhone = req.getParameter("shopPhone");
            String shopProvince = req.getParameter("shopProvince");
            String shopCity = req.getParameter("shopCity");
            String shopDistrict = req.getParameter("shopDistrict");
            String shopAddress = req.getParameter("shopAddress");
            String shopLongitude = req.getParameter("shopLongitude");
            String shopLatitude = req.getParameter("shopLatitude");

            Shop shop = new Shop(
                    null,
                    shopName,
                    shopDescribe,
                    shopImage,
                    shopPhone,
                    shopProvince,
                    shopCity,
                    shopDistrict,
                    shopAddress,
                    shopLongitude,
                    shopLatitude

            );
            int row = shopDao.insertShop(shop);
            if (row!=0){
                Result success = Result.success();
                resp.getWriter().write(JSON.toJSONString(success));
            }else {
                Result error = Result.error();
                resp.getWriter().write(JSON.toJSONString(error));
            }
        }else if ("modifyShop".equals(action)){
            Integer shopId = Integer.parseInt(req.getParameter("shopId"));
            String shopName = req.getParameter("shopName");
            String shopDescribe = req.getParameter("shopDescribe");
            String shopImage = req.getParameter("shopImage");
            String shopPhone = req.getParameter("shopPhone");
            String shopProvince = req.getParameter("shopProvince");
            String shopCity = req.getParameter("shopCity");
            String shopDistrict = req.getParameter("shopDistrict");
            String shopAddress = req.getParameter("shopAddress");
            String shopLongitude = req.getParameter("shopLongitude");
            String shopLatitude = req.getParameter("shopLatitude");
            Shop shop = new Shop(
                    shopId,
                    shopName,
                    shopDescribe,
                    shopImage,
                    shopPhone,
                    shopProvince,
                    shopCity,
                    shopDistrict,
                    shopAddress,
                    shopLongitude,
                    shopLatitude

            );
            int row = shopDao.updateShop(shop);
            if (row!=0){
                Result success = Result.success();
                resp.getWriter().write(JSON.toJSONString(success));
            }else {
                Result error = Result.error();
                resp.getWriter().write(JSON.toJSONString(error));
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
